<template>
  <div class="file-copy-dialog">
    <el-form :model="Copy" size="mini" label-width="100px" label-position="left">
      <el-form-item label="来源地址">
        <data-source-input
            style="width: 89%;margin-left: 6px"
            :datasource-item="Copy.Read"
            :node-id="Copy.Read.DataSourceId"
            :param-name="Copy.Read.ParamName"
            @change="onChangeRead"
        />
      </el-form-item>
      <el-form-item label="目标地址">
        <data-source-input
            style="width: 89%;margin-left: 6px"
            :datasource-item="Copy.Write"
            :node-id="Copy.Write.DataSourceId"
            :param-name="Copy.Write.ParamName"
            @change="onChangeWrite"
        />
      </el-form-item>
      <el-form-item label="文件名称">
        <data-source-input
            style="width: 89%;margin-left: 6px" placeholder="文件名称,不填则同名"
            :datasource-item="Copy.Write.FileName"
            :node-id="Copy.Write.FileName.DataSourceId"
            :param-name="Copy.Write.FileName.ParamName"
            @change="onChangeFileName"
        />
      </el-form-item>
      <el-form-item label="描述">
        <el-input
            v-model="Copy.Desc"
            type="textarea"
            :rows="3"
            placeholder="请输入描述"
            style="width: 90%"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import DataSourceInput from "@/vue2-flow/src/views/flow/components/NodeInputSourceDataComponent.vue";
import {onMounted, ref} from "vue";
//@ts-ignore
import Utils from "@/vue2-flow/src/utils/Common.js";
import {useFlowStore} from "@/store/modules/flow/flowStore";
//@ts-ignore
import FlowNodeDataApi from "@/api/flow/FlowNodeDataApi";

const FlowStore = useFlowStore();

interface CopyType {
  Read: {
    DataSourceId: number,
    ParamName: string,
    DataSource: any
  },
  Write: {
    DataSourceId: number,
    ParamName: string,
    DataSource: any,
    FileName: {
      DataSourceId: number,
      ParamName: string,
      DataSource: any
    }
  },
  Desc: string
}

const Copy = ref<CopyType>(
    {
      Read: {DataSourceId: 0, DataSource: null, ParamName: ''},
      Write: {
        DataSourceId: 0,
        DataSource: null,
        ParamName: '',
        FileName: {DataSourceId: 0, DataSource: null, ParamName: ''}
      },
      Desc: ''
    }
)

const onChangeRead = (ReadItem: any) => {
  if (!Utils.IsEmpty(ReadItem)) {
    if (!Copy.value.Read) {
      Copy.value.Read = {DataSourceId: 0, DataSource: null, ParamName: ''}
    }
    Copy.value.Read.DataSourceId = ReadItem.nodeId
    Copy.value.Read.ParamName = ReadItem.ParamName
    Copy.value.Read.DataSource = ReadItem.DataSource
  }
}

const onChangeWrite = (WriteItem: any) => {
  if (!Utils.IsEmpty(WriteItem)) {
    if (!Copy.value.Write) {
      Copy.value.Write = {
        DataSourceId: 0,
        DataSource: null,
        ParamName: '',
        FileName: {DataSourceId: 0, DataSource: null, ParamName: ''}
      }
    }
    Copy.value.Write.DataSourceId = WriteItem.nodeId
    Copy.value.Write.ParamName = WriteItem.ParamName
    Copy.value.Write.DataSource = WriteItem.DataSource
  }
}

const onChangeFileName = (FileNameItem: any) => {
  if (!Utils.IsEmpty(FileNameItem)) {
    if (!Copy.value.Write.FileName) {
      Copy.value.Write.FileName = {DataSourceId: 0, DataSource: null, ParamName: ''}
    }
    Copy.value.Write.FileName.DataSourceId = FileNameItem.nodeId
    Copy.value.Write.FileName.ParamName = FileNameItem.ParamName
    Copy.value.Write.FileName.DataSource = FileNameItem.DataSource
  }
}

const _SaveNode = (NodeName: string) => {
  const _file = Object.assign({}, Copy.value)
  new FlowNodeDataApi().Install(FlowStore.CurrentClickNode.id, {NodeFile: _file}, NodeName)
}

const SetFormValue = (_node: any) => {
  Copy.value = _node?.nodeInputParam?.NodeFile ?? Copy.value
}

onMounted(() => {
  SetFormValue(FlowStore.CurrentClickNode)
})

defineExpose({_SaveNode})
</script>

<style scoped lang="scss">
.file-copy-dialog {

}
</style>
